package com.zxl.working.service.impl;

import com.zxl.working.entity.backend.AgentAccount;
import com.zxl.working.entity.backend.CpmInsideTrans;
import com.zxl.working.entity.backend.SellerAccount;
import com.zxl.working.mapper.backend.TaskMapper;
import com.zxl.working.service.TaskService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    private Logger logger = LogManager.getLogger(TaskServiceImpl.class);
    @Resource
    private TaskMapper taskMapper;


    /**
     * 内部结算交易统计
     */
    @Override
    @Transactional
    public void recordTrans() {
        logger.info("内部结算交易统计 开始，TaskServiceImpl--->recordTrans");
        List<CpmInsideTrans> allList = new ArrayList<>();
        allList.addAll(taskMapper.qryAgentMonthFeeTrans());
        logger.info("内部结算交易统计 代理商月结佣金，recordTrans--->qryAgentMonthFeeTrans");
        allList.addAll(taskMapper.qryAgentStepFeeTrans());
        logger.info("内部结算交易统计 代理商阶梯佣金，recordTrans--->qryAgentStepFeeTrans");
        allList.addAll(taskMapper.qrySellerSendTrans());
        logger.info("内部结算交易统计 商户发放费用，recordTrans--->qrySellerSendTrans");
        allList.addAll(taskMapper.qryServerFeeTrans());
        logger.info("内部结算交易统计 服务商服务费,返回值："+allList.toString()+"，recordTrans--->qryServerFeeTrans");

        if(allList.size() > 0) {
            int re = taskMapper.batchInsertCpmInsideTrans(allList);
            logger.info("内部结算交易统计 内部结算表插入，recordTrans--->batchInsertCpmInsideTrans");
        }
        taskMapper.updSellerProvideOrder();
        logger.info("内部结算交易统计 更新订单状态，recordTrans--->updSellerProvideOrder");
        logger.info("内部结算交易统计 结束，TaskServiceImpl--->recordTrans");
    }

    /**
     * 插入商户订单明细
     */
    @Override
    @Transactional
    public void insSellerProvideOrder() {
        logger.info("插入商户订单明细 开始，TaskServiceImpl--->insSellerProvideOrder");
        taskMapper.insSellerProvideOrder();
        logger.info("插入商户订单明细 插入商户明细表，insSellerProvideOrder--->insSellerProvideOrder");
        taskMapper.updInsideTrans(0);
        logger.info("插入商户订单明细 更新内部结算表，insSellerProvideOrder--->updInsideTrans");
        logger.info("插入商户订单明细 结束，TaskServiceImpl--->insSellerProvideOrder");
    }

    /**
     * 更新账户汇总表
     */
    @Override
    public void updSellerAccount() {
        logger.info("更新账户汇总表 开始，TaskServiceImpl--->updSellerAccount");
        taskMapper.insSellerAccountHistory();
        logger.info("更新账户汇总表 保存有未汇总明细的虚拟账号历史完成，updSellerAccount--->insSellerAccountHistory");
        List<SellerAccount> accounts = taskMapper.qryAccountData();
        logger.info("更新账户汇总表 获取虚拟账号汇总数据,返回值："+accounts.toString()+"，updSellerAccount--->qryAccountData");
        if(accounts.size() > 0) {
            accounts.stream().forEach(item -> taskMapper.updSellerAccount(item));
            logger.info("更新账户汇总表 更新虚拟账号历史，updSellerAccount--->updSellerAccount");
        }
        int r1 = taskMapper.upSellerAccountDetail();
        logger.info("更新账户汇总表 关闭已经统计的虚拟账号流水，updSellerAccount--->upSellerAccountDetail");

        logger.info("更新账户汇总表 结束，TaskServiceImpl--->updSellerAccount");
    }

    /**
     * 更新代理商账户表
     */
    public void updAgentAccount(){
        logger.info("更新账户明细表 开始，TaskServiceImpl--->updAgentAccount");
        taskMapper.insAgentAccountDetail();
        taskMapper.updInsideTrans(1);
        logger.info("更新账户明细表 结束，TaskServiceImpl--->updAgentAccount");

        logger.info("更新账户汇总表 开始，TaskServiceImpl--->updAgentAccount");
        taskMapper.insAgentAccountHis();
        List<AgentAccount> accounts = taskMapper.qryAccounts();
        logger.info("更新账户汇总表 获取数据："+accounts.toString()+"，TaskServiceImpl--->updAgentAccount");
        if(accounts.size() > 0) {
            accounts.stream().forEach(item -> taskMapper.updAgentAccount(item));

        }
        taskMapper.updAgentAccountState();
        logger.info("更新账户汇总表 结束，TaskServiceImpl--->updAgentAccount");
    }

}
